package com.niuwa.p2p.service.impl.custService;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.dao.custservice.CustServiceTraceLogDao;
import com.niuwa.p2p.dao.custservice.CustServiceUserDao;
import com.niuwa.p2p.dao.role.RoleDao;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.custservice.CustServiceTraceLog;
import com.niuwa.p2p.entity.custservice.CustServiceUser;
import com.niuwa.p2p.entity.role.Role;
import com.niuwa.p2p.service.custService.ICustServiceService;
import com.niuwa.p2p.vo.customerservice.CustomerServiceJobUserVo;

/**
 * @author futianliang
 * @date 2015年4月28日
 */
@Service
public class CustServiceServiceImpl implements ICustServiceService {
	private final static String A = "OB经理";
	private final static String B = "OB组长";
	private final static String C = "OB顾问";

	@Autowired
	private CustServiceTraceLogDao custDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private CustServiceUserDao custServiceUserDao;
	

	@Override
	public List<CustServiceTraceLog> getCustServiceTraceLogList(Map<String, Object> paramMap) {
		return custDao.getCustServiceTraceLogList(paramMap);
	}

	@Override
	public int getCustServiceTraceLogListCount(Map<String, Object> paramMap) {
		return custDao.getCustServiceTraceLogListCount(paramMap);
	}

	@Override
	public PageBean<CustServiceUser> getCustList(Map paramMap, long userId, String loginName) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		// 获取用户角色
		List<Role> list = roleDao.searchRolesByloginName(loginName);
		if (!isContainsThisRole(list, A)) {// 没有经理权限
			if (isContainsThisRole(list, B)) {// 小组长
				paramMap.put("groupUserId", userId);
			} else if (isContainsThisRole(list, C)) {// 顾问
				paramMap.put("userId", userId);
			}
		}
		Integer totalCount = custServiceUserDao.getCustServiceListCount(paramMap);
		if (totalCount == null) {
			totalCount = 0;
		}
		PageBean<CustServiceUser> page = new PageBean<CustServiceUser>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		page.setRows(custServiceUserDao.getCustServiceList(paramMap));
		return page;
	}

	private boolean isContainsThisRole(List<Role> roleList, String roleName) {
		boolean result = false;
		if (roleList != null && roleList.size() > 0) {
			for (Role role : roleList) {
				if (roleName.equals(role.getRoleName())) {
					result = true;
					break;
				}
			}
		}
		return result;
	}
	

	@Override
	public CustServiceUser selectByPrimaryKey(Long id) {
		return custServiceUserDao.selectByPrimaryKey(id);
	}

	@Override
	public List<CustomerServiceJobUserVo> getCustomerWithNoAccout() {
		// TODO Auto-generated method stub
		return custServiceUserDao.getCustomerWithNoAccout();
	}

	@Override
	public List<CustomerServiceJobUserVo> getAccoutWithOutInvest() {
		// TODO Auto-generated method stub
		return custServiceUserDao.getAccoutWithOutInvest();
	}

	@Override
	public void insertBatch(List<? extends CustServiceUser> custServiceUsers) {
		custServiceUserDao.insertBatch(custServiceUsers);
	}


	@Override
	public List<CustServiceUser> getByMap(Map map) {
		return custServiceUserDao.selectByMap(map);
	}



	@Override
	public void update(CustServiceUser custServiceUser) {
		custServiceUserDao.updateByPrimaryKey(custServiceUser);
		
	}

	@Override
	public List<CustServiceUser> getCustServiceAndCustomerWithTel() {
		return custServiceUserDao.getCustServiceAndCustomerWithTel();
	}


	@Override
	public List<CustServiceUser> getCustServiceUseByTel(
			Map<String, Object> parammap) {
		
		return custServiceUserDao.getCustServiceUseByTel(parammap);
	}

	@Override
	public void addCustServiceUser(Map<String, Object> parammap) {
		custServiceUserDao.addCustServiceUse(parammap);
		
	}

}
